package com.mike.bdb.ui.provider.table;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;

import org.eclipse.jface.viewers.IStructuredContentProvider;
import org.eclipse.jface.viewers.Viewer;
import org.eclipse.swt.widgets.Button;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.mike.bdb.manager.DBManager;
import com.mike.bdb.util.DataCurrent;
import com.mike.bdb.util.ReflectUtil;
import com.mike.bdb.util.DPLBDBNameUtil.KeyName;
import com.sleepycat.persist.EntityCursor;
import com.sleepycat.persist.EntityStore;
import com.sleepycat.persist.SecondaryIndex;

public class MikeTableContentProvider implements IStructuredContentProvider {

	private static final Logger LOG = LoggerFactory.getLogger(MikeTableContentProvider.class);
	
	@Override
	public Object[] getElements(Object inputElement) {
		KeyName keyName = (KeyName) inputElement;
		
		String curStoreClazz = DataCurrent.getInstance().curStoreClazz;
		Class<Object> classBy = ReflectUtil.getClassBy(curStoreClazz);
		if(null == classBy) return new Object[0];
		EntityCursor<?> entities = null;
		EntityStore store = DBManager.getInstance().getEntityStore();
		PageInfo pageInfo = DataCurrent.getInstance().getPageInfo();
			if(keyName.isSec()) {
				SecondaryIndex<String, String, ?> secondaryIndex = 
					store.getSecondaryIndex(
							store.getPrimaryIndex(String.class, classBy),
							String.class,
							keyName.getSuffix());
				entities = secondaryIndex.entities();
			} else {
				
				
				if (null == pageInfo) return null;
				
				if(LOG.isDebugEnabled()) {
					LOG.debug("PageInfo:" + pageInfo.toString());
				}
				Class pkClass = ReflectUtil.getPKClass(classBy);
				//TODO ★ Berkeley DB entities
				entities = store.getPrimaryIndex(pkClass, classBy).entities();
			}
		try {
			
			List<Object> result = new ArrayList<Object>();
			
			int index = 0;
			
			for (Object o : entities) {
				
				//1.不足
				if(index < pageInfo.fromInt()) {
					++index;
					continue;
				}
				
				//2.命中 
				if(index >= pageInfo.fromInt() && index < pageInfo.toInt()) {
					result.add(o);
					++index;
				}
				
				//3.越界
				if(index >= pageInfo.toInt()) {
					++index;
					break;
				}
			}
			if(LOG.isDebugEnabled()) {
				LOG.debug(classBy + " size:" + result.size());
			}
			
			return result.toArray();
		} finally {
			entities.close();
		}
	}
	
	/** 清除创建的二级索引CRUD按钮 */
	public static void cleanButton() {
		Map<String, Button> tableButtonMap = MikeTableLabelProvider.tableButtonMap;
		Iterator<Entry<String, Button>> iterator = tableButtonMap.entrySet().iterator();
		while(iterator.hasNext()) {
			Entry<String, Button> next = iterator.next();
			Button btn = next.getValue();
			if(!btn.isDisposed())
				btn.dispose();
		}
		tableButtonMap.clear();
	}
	
	@Override
	public void dispose() {
	}

	@Override
	public void inputChanged(Viewer viewer, Object obj1, Object obj2) {
		cleanButton();
	}

	
}
